package com.example.dataservice.serviceimpl;

import com.example.common.constants.YLBConstant;
import com.example.common.util.CommonUtil;
import com.example.dataservice.mapper.ProductInfoMapper;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.api.model.ProductInfo;
import org.example.api.pojo.MultiProduct;
import org.example.api.service.ProductInfoService;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/12 下午 04:28
 * @version: v1.0
 */
@DubboService(interfaceClass = ProductInfoService.class, version = "1.0")
public class ProductInfoServiceImpl implements ProductInfoService {
    @Resource
    private ProductInfoMapper productInfoMapper;

    /*按产品类型分类查询产品*/
    @Override
    public List<ProductInfo> queryByTypeLimit(Integer productType, Integer pageNo, Integer pageSize) {

        List<ProductInfo> productInfos = new ArrayList<>();
        if (productType == 0 || productType == 1 || productType == 2){
            pageNo = CommonUtil.defaultPageNO(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            int offset = (pageNo - 1) * pageNo;
            productInfos = productInfoMapper.selectByTypeLimit(productType, offset, pageSize);

        }

        return productInfos;
    }
    /* 某个产品的记录总数*/
    @Override
    public Integer queryRecordNumsByType(Integer productType) {
        Integer counts = 0;
        if (productType == 0 || productType == 1 || productType ==2 ){
            counts = productInfoMapper.selectCountByType(productType);
        }
        return counts;
    }

    /*产品的多个产品数据*/
    @Override
    public MultiProduct queryIndexPageProducts() {
        MultiProduct result = new MultiProduct();

        // 新手包
        List<ProductInfo> xinShouBao = productInfoMapper.selectByTypeLimit(YLBConstant.PRODUCT_TYPE_XINSHOUBAO,0,1 );
        // 优选
        List<ProductInfo> youXuan = productInfoMapper.selectByTypeLimit(YLBConstant.PRODUCT_TYPE_YOUSUAN,0,3);
        // 散标
        List<ProductInfo> sanBiao = productInfoMapper.selectByTypeLimit(YLBConstant.PRODUCT_TYPE_SANBIAO,0,3);
        result.setSanBao(sanBiao);
        result.setXinShoubao(xinShouBao);
        result.setYouXuan(youXuan);
        return result;
    }

    /*查询所有产品的信息*/
    @Override
    public List<ProductInfo> queryAllProduct() {
        List<ProductInfo> productInfos = productInfoMapper.selectAll();
        return productInfos;
    }
    /*根据产品id查询产品信息*/
    @Override
    public ProductInfo queryById(Integer id) {
        ProductInfo productInfo = null;
        if (id != null && id > 0){
            productInfo = productInfoMapper.selectByPrimaryKey(id);
        }
        return productInfo;
    }


}
